package com.polaris.live.ui.party.widget.videoseat

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.polaris.live.R.color
import com.polaris.live.R.drawable
import com.polaris.live.common.constant.PagConst
import com.polaris.live.databinding.WidgetVideoSeatItemBinding
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.interface_bag.PAGListener
import com.polaris.live.resp.back_resp.UserRoomThemeResp
import com.polaris.live.translation.R
import com.polaris.live.ui.party.widget.videoseat.base.BaseVideoSeatItemView
import com.polaris.live.ui.party.widget.videoseat.bean.VideoSeatBean
import com.polaris.live.utils.RichCharmImgUtil
import com.polaris.live.utils.file.FileUtils
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils
import org.libpag.PAGFile
import org.libpag.PAGImageView
import java.io.File

/**
 * 视频房 单个视频item
 * @author Created by 天晴 on 2024/6/21 17:00
 * @since 1.0.0
 **/
class VideoSeatItemView(context: Context, attrs: AttributeSet?) :
    BaseVideoSeatItemView(context, attrs) {

    private val mBinding = WidgetVideoSeatItemBinding.inflate(LayoutInflater.from(context), this)

    //麦位皮肤图标
    private var seatStateSkinUrl: String? = null

    //麦位皮肤锁定图标
    private var seatLockSkinUrl: String? = null

    init {
        mBinding.pagEmoji.apply {
            setRepeatCount(1)
            addListener(object : PAGListener {
                override fun onAnimationEnd(p0: PAGImageView?) {
                    super.onAnimationEnd(p0)
                    visibility = View.GONE
                }

                override fun onAnimationStart(p0: PAGImageView?) {
                    super.onAnimationStart(p0)
                    visibility = View.VISIBLE
                }
            })
        }
    }

    override fun initView() {
        mBinding.seatidTv.text = "${mSeatId + 1}"
    }

    /**
     * 这个座位上得声浪变化
     */
    override fun soundLevelUpdate(soundLevel: Float) {
        if (soundLevel > 2) {
            mBinding.mike.setImageResource(drawable.party_video_add_mike_speak)
            showPag()
        } else {
            if (seatBean.siteMicStatus != 1) {
                mBinding.mike.setImageResource(drawable.party_video_add_mike_close)
            } else {
                mBinding.mike.setImageResource(0)
            }
            pausePag()
        }
    }

    override fun upDataSoundPath(soundPath: String) {
        if (soundPath == PagConst.LIVE_PARTY_SEAT) {
            mBinding.pag.apply {
                path = soundPath
                setRepeatCount(-1)
            }
        } else {
            val pageFile = PAGFile.Load(soundPath)
            mBinding.pag.apply {
                setRepeatCount(-1)
                composition = pageFile
                setRepeatCount(-1)
            }

        }
    }

    override fun upDateSkinView(userRoomThemeResp: UserRoomThemeResp?) {
        if (userRoomThemeResp != null) {
            seatStateSkinUrl = ImageUtils.imgPath(userRoomThemeResp.prefix + userRoomThemeResp.site)
            seatLockSkinUrl = ImageUtils.imgPath(userRoomThemeResp.prefix + userRoomThemeResp.siteLock)
        } else {
            seatStateSkinUrl = null
            seatLockSkinUrl = null
        }
        setLock()
    }

    /**
     * 更新数据
     */
    override fun upDataView(mData: VideoSeatBean, isReset: Boolean) {
        val userBasic = mData.userBasicVo
        if (userBasic != null) {
            mBinding.seatStateImg.visibility = GONE
            if (mData.siteVideoStatus == 1) {
                mBinding.avFv.visibility = GONE
                mBinding.pag.visibility = GONE
            } else {
                setAvatar(userBasic)
            }
            setName(userBasic)
            if (mData.siteVideoStatus == 1) {
                mBinding.videoFl.visibility = VISIBLE
            } else {
                mBinding.videoFl.visibility = GONE
            }
            setGainCoin(mData.siteGainCoin)
            setMike(mData.siteMicStatus == 1)
        } else {
            noUser()
            if (!isReset) {
                setLock()
            }
        }
        pausePag()
    }

    private fun setLock() {
        if (mSiteLock) {
            if (!seatLockSkinUrl.isNullOrEmpty()) {
                Glide.with(context).load(seatLockSkinUrl).into(mBinding.seatStateImg)
            } else {
                mBinding.seatStateImg.setImageResource(drawable.party_video_lock)
            }
        } else {
            if (!seatStateSkinUrl.isNullOrEmpty()) {
                Glide.with(context).load(seatStateSkinUrl).into(mBinding.seatStateImg)
            } else {
                mBinding.seatStateImg.setImageResource(drawable.party_video_add)
            }
        }
    }

    override fun showEmoji(message: SendLiveMessage) {
        val pagPath = FileUtils.getFilePath(FileUtils.getPagFile(), message.content)
        if (!File(pagPath).exists()) {
            return
        }
        mBinding.pagEmoji.apply {
            visibility = View.VISIBLE
            path = pagPath
            play()
        }
    }

    override fun getTextureView() = mBinding.videoFl
    override fun upDataUserPosition(position: Int) {
    }

    private fun setName(userBasicVo: UserBasicVo) {
        mBinding.apply {
            nameTv.text = userBasicVo.nickname
            val color =
                RichCharmImgUtil.serColor2HexColor(userBasicVo.nicknameColor) ?: ColorUtils.getColor(color.white)
            nameTv.setTextColor(color)
        }

    }

    private fun setMike(b: Boolean) {
        if (!b) {
            mBinding.mike.setImageResource(drawable.party_video_add_mike_close)
        } else {
            mBinding.mike.setImageResource(0)
        }
    }

    /**
     * 设置头像和头像框
     */
    private fun setAvatar(userBasicVo: UserBasicVo) {
        mBinding.apply {
            avFv.visibility = VISIBLE
            avatarImg.setImageUrl(userBasicVo.avatar, userBasicVo.userId)
            avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(userBasicVo.avatarFrame))
        }
    }

    private fun setGainCoin(siteGainCoin: Long?) {
        mBinding.beansLl.visibility = VISIBLE
        mBinding.giftTv.text = "${siteGainCoin ?: 0}"
    }

    private fun noUser() {
        mBinding.apply {
            beansLl.visibility = GONE
            videoFl.visibility = GONE
            seatStateImg.visibility = VISIBLE
            nameTv.text = StringUtils.getString(R.string.name_no, mSeatId + 1)
            nameTv.setTextColor(ColorUtils.getColor(color.white))
            giftTv.text = "0"
            avFv.setFrameImage(null)
            avFv.visibility = GONE
            avatarImg.setImageResource(0)
            mike.setImageResource(0)
        }
    }

    private fun pausePag() {
        if (mBinding.avFv.visibility == VISIBLE) {
            if (mBinding.pag.visibility != INVISIBLE) {
                mBinding.pag.visibility = INVISIBLE
            }
            if (mBinding.pag.isPlaying) {
                mBinding.pag.pause()
            }
        }
    }

    private fun showPag() {
        if (mBinding.avFv.visibility == VISIBLE) {
            if (mBinding.pag.visibility != VISIBLE) {
                mBinding.pag.visibility = VISIBLE
            }
            if (!mBinding.pag.isPlaying) {
                mBinding.pag.play()
            }
        }
    }
}